﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using System.Data;
using IDAL;
using DAL;
using System.Web;
using Models;

namespace BLL
{
    public class NewsInfoLogic
    {
        public static INewsInfo newsinfo = new NewsInfoDAL();

        public static List<NewsInfo> GetAllNewsInfo(int pagesize, int nowpage)
        {
            List<NewsInfo> list = new List<NewsInfo>();
            list = newsinfo.GetAllNewsInfo(pagesize, nowpage);
            list = FormatNewsInfoList(list);
            for (int i = 0; i < list.Count; i++)
            {
                //process news show id in admin panel
                list[i].ShowID = i + 1 + (nowpage - 1) * pagesize;

                //process news content
                if (list[i].NewsBrief == "")
                {
                    string oldString = list[i].NewsContent;
                    string newString = DeleteHTML(oldString);

                    if (newString.Length > 170)
                    {
                        list[i].NewsBrief = newString.Substring(0, 170) + "……";
                    }
                    else
                    {
                        list[i].NewsBrief = newString;
                    }
                }
            }
            return list;
        }

        public static List<NewsInfo> GetNewsInfo(int categoryID)
        {
            List<NewsInfo> list = newsinfo.GetNewsInfo(categoryID);
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].PublishTime != null)
                {
                    list[i].ShowTime = list[i].PublishTime.ToShortDateString();
                }
            }
            return list;
        }

        public static int GetPageCount(int pagesize, int CategoryID)
        {
            return newsinfo.GetPageCount(pagesize,CategoryID);
        }

        public static List<NewsInfo> GetPageNewsInfo()
        {
            List<NewsInfo> list = newsinfo.GetPageNewsInfo();
            list = FormatNewsInfoList(list);
            for (int i = 0; i < list.Count; i++)
            {
                //process news content
                string oldString = list[i].NewsContent;
                string newString = DeleteHTML(oldString);
                if (newString.Length > 65)
                {
                    list[i].NewsContent = newString.Substring(0, 65) + "……";
                }
                else
                {
                    list[i].NewsContent = newString;
                }
            }
            return list;
        }
        public static int AddNewsInfo(NewsInfo news)
        {
            int isSuccess = -1;
            isSuccess = newsinfo.AddNewsInfo(news);
            return isSuccess;
        }
        public static int UpdateNewsInfo(NewsInfo news)
        {
            int isSuccess = -1;
            isSuccess = newsinfo.UpdateNewsInfo(news);
            return isSuccess;
        }
        public static NewsInfo GetOneNewsInfo(int NewsID)
        {
            return newsinfo.GetOneNewsInfo(NewsID);
        }
        public static List<NewsInfo> GetCategoryNewsInfo(int CategoryID, int pagesize, int nowpage)
        {
            List<NewsInfo> list = newsinfo.GetCategoryNewsInfo(CategoryID, pagesize, nowpage);
            list = FormatNewsInfoList(list);
            for (int i = 0; i < list.Count; i++)
            {
                //process news content
                if (list[i].NewsBrief == "")
                {
                    string oldString = list[i].NewsContent;
                    string newString = DeleteHTML(oldString);

                    if (newString.Length > 170)
                    {
                        list[i].NewsBrief = newString.Substring(0, 170) + "……";
                    }
                    else
                    {
                        list[i].NewsBrief = newString;
                    }
                }
            }

            return list;
        }

        public static ArrayList GetPreNewsInfo(int nowNewsID,int categoryID)
        {
            return newsinfo.GetPreNewsInfo(nowNewsID,categoryID);
        }

        public static ArrayList GetNextNewsInfo(int nowNewsID,int categoryID)
        {
            return newsinfo.GetNextNewsInfo(nowNewsID,categoryID);
        }

        public static List<NewsInfo> FormatNewsInfoList(List<NewsInfo> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                //process datatime
                if (list[i].PublishTime != null)
                {
                    list[i].ShowTime = list[i].PublishTime.ToShortDateString();
                }
                
                //process news hot flag
                if (list[i].HotFlag == 1)
                {
                    list[i].NewsHot = "<div class='newshot'><img src='images/bghotn.png' alt='hot news' /></div>";
                }
                else
                {
                    list[i].NewsHot = "";
                }
                //process news new flag
                if (list[i].NewFlag == 1)
                {
                    list[i].NewsNew = "newsnf";
                }
                else
                {
                    list[i].NewsNew = "";
                }
            }
            return list;
        }

        
        public static string DeleteHTML(string Htmlstring)
        {
            string[] aryReg ={   
                      @"<script[^>]*?>.*?</script>",   
                      @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",   
                      @"([\r\n])[\s]+",   
                      @"&(quot|#34);",   
                      @"&(amp|#38);",   
                      @"&(lt|#60);",   
                      @"&(gt|#62);",     
                      @"&(nbsp|#160);",     
                      @"&(iexcl|#161);",   
                      @"&(cent|#162);",   
                      @"&(pound|#163);",   
                      @"&(copy|#169);",   
                      @"&#(\d+);",   
                      @"-->",   
                      @"<!--.*\n" ,
                      @"[ ]+"
                    };

            string[] aryRep = {   
                        "",   
                        "",   
                        "",   
                        "\"",   
                        "&",   
                        "<",   
                        ">",   
                        "   ",   
                        "\xa1",//chr(161),   
                        "\xa2",//chr(162),   
                        "\xa3",//chr(163),   
                        "\xa9",//chr(169),   
                        "",   
                        "\r\n",   
                        "" ,
                        " "
                      };

            string newReg = aryReg[0];
            string strOutput = Htmlstring;
            for (int i = 0; i < aryReg.Length; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }
            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            return strOutput;
        }

        public static int NewsInfoCountAdd(int NewsID)
        {
            return newsinfo.NewsInfoCountAdd(NewsID);
        }

        /// <summary>
        /// 删除指定的新闻
        /// </summary>
        /// <param name="NewsID"></param>
        /// <returns></returns>
        public static int DeleteSomeNewsInfo(int[] NewsID)
        {
            if(NewsID.Count() <= 0)
            {
                return -1;
            }
            foreach (var item in NewsID)
            {
                if ( newsinfo.DeleteNewsInfo(item) != 1){
                    return -1;
                } 
            }
            return 1;
        }

        public static int UpdateNewsInfoHotFlag(int[] NewsID,bool flag)
        {
            foreach (var item in NewsID)
            {
                NewsInfo news = newsinfo.GetOneNewsInfo(item);
                if(news == null){
                    return -1;
                }
                news.HotFlag = flag ? 1 : 0;
                newsinfo.UpdateNewsInfo(news);
            }
            return 1;
        }

        public static int GetCategoryNewsInfoCount(int CategoryID)
        {
            return newsinfo.GetCategoryNewsInfoCount(CategoryID);
        }
        public static int DeleteOneNewsInfo(int NewsID)
        {
            return newsinfo.DeleteNewsInfo(NewsID);
        }
    }
}
